package com.risk.management.service.impl;

import com.risk.management.entity.RiskResponse;
import com.risk.management.mapper.RiskResponseMapper;
import com.risk.management.service.RiskResponseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.time.LocalDateTime;

/**
 * 风险响应计划服务实现类
 */
@Service
public class RiskResponseServiceImpl implements RiskResponseService {

    @Autowired
    private RiskResponseMapper riskResponseMapper;

    @Override
    public RiskResponse getRiskResponseById(Long id) {
        return riskResponseMapper.selectById(id);
    }

    @Override
    public List<RiskResponse> getRiskResponsesByRiskId(Long riskId) {
        return riskResponseMapper.selectByRiskId(riskId);
    }

    @Override
    public List<RiskResponse> getRiskResponsesByResponsibleId(Long responsibleId) {
        return riskResponseMapper.selectByResponsibleId(responsibleId);
    }

    @Override
    public List<RiskResponse> getRiskResponsesByStatus(Integer status) {
        return riskResponseMapper.selectByStatus(status);
    }

    @Override
    @Transactional
    public boolean addRiskResponse(RiskResponse riskResponse) {
        // 设置创建和更新时间
        Date now = new Date();
        riskResponse.setCreateTime(now);
        riskResponse.setUpdateTime(now);
        
        // 如果状态为空，设置为未开始状态
        if (riskResponse.getStatus() == null) {
            riskResponse.setStatus(1); // 1表示未开始
        }
        
        return riskResponseMapper.insert(riskResponse) > 0;
    }

    @Override
    @Transactional
    public boolean updateRiskResponse(RiskResponse riskResponse) {
        // 检查响应计划是否存在
        RiskResponse existingResponse = getRiskResponseById(riskResponse.getId());
        if (existingResponse == null) {
            return false;
        }
        
        // 设置更新时间
        riskResponse.setUpdateTime(new Date());
        
        // 如果状态为已完成，但实际完成时间为空，设置为当前时间
        if (riskResponse.getStatus() != null && riskResponse.getStatus() == 3 && riskResponse.getActualEndTime() == null) {
            riskResponse.setActualEndTime(new Date());
        }
        
        return riskResponseMapper.update(riskResponse) > 0;
    }

    @Override
    @Transactional
    public boolean deleteRiskResponse(Long id) {
        // 检查响应计划是否存在
        RiskResponse riskResponse = getRiskResponseById(id);
        if (riskResponse == null) {
            return false;
        }
        
        return riskResponseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional
    public boolean updateRiskResponseStatus(Long id, Integer status) {
        // 检查响应计划是否存在
        RiskResponse riskResponse = getRiskResponseById(id);
        if (riskResponse == null) {
            return false;
        }
        
        // 检查状态是否有效（1-未开始，2-进行中，3-已完成）
        if (status < 1 || status > 3) {
            return false;
        }
        
        // 如果状态更新为已完成，更新实际完成时间
        if (status == 3 && (riskResponse.getStatus() == null || riskResponse.getStatus() != 3)) {
            updateRiskResponseActualEndTime(id, new Date());
        }
        
        return riskResponseMapper.updateStatus(id, status) > 0;
    }

    @Override
    @Transactional
    public boolean updateRiskResponseActualEndTime(Long id, Date actualEndTime) {
        // 检查响应计划是否存在
        RiskResponse riskResponse = getRiskResponseById(id);
        if (riskResponse == null) {
            return false;
        }
        
        return riskResponseMapper.updateActualEndTime(id, actualEndTime) > 0;
    }

    @Override
    @Transactional
    public int checkAndUpdateOverdueResponses() {
        LocalDateTime now = LocalDateTime.now();
        return riskResponseMapper.updateOverdueResponses(now);
    }
} 